package com.tust.androidreader;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

public class StartActivity extends AppCompatActivity {
    private boolean hasStep = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();//隐藏标题导航栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去除时间和电量等状态栏
        setContentView(R.layout.activity_start);

        LinearLayout linearLayoutStep = this.findViewById(R.id.linear_step);

        linearLayoutStep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//点击”跳过“,跳转到LoginActivity
                Intent intent = new Intent(StartActivity.this,LoginActivity.class);
                startActivity(intent);
                hasStep = true;
            }
        });

        DBHelper dbHelper = new DBHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        db.execSQL(//文本文件需要保存为UTF-8
            "create table if not exists book(" +
                    "bookicon int," +
                    "bookname varchar(30) not null primary key," +
                    "author varchar(30) not null ," +
                    "likenum varchar(30) not null ," +
                    "readtimes varchar(30) not null ," +
                    "commentnum varchar(30) not null," +
                    "classify varchar(30) not null ," +
                    "addtime varchar(30) not null," +
                    "wordnum varchar(30) not null," +
                    "bookcontent varchar(30) not null," +
                    "currentpage int not null)");

        Cursor bookCursor = db.rawQuery("select * from book", null);
        if (bookCursor.getCount() < 1) {//book表中没有记录
            db.execSQL("insert into book values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{R.drawable.b1, "老人与海", "海明威", "0", "0", "0", "外国名著", "2022/5/16", "47731", R.raw.b1, 0});
            db.execSQL("insert into book values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{R.drawable.b2, "孙子兵法", "孙武", "0", "0", "0", "古典名著", "2022/5/16", "22564", R.raw.b2, 0});
            db.execSQL("insert into book values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{R.drawable.b3, "三体：地球往事", "刘慈欣", "0", "0", "0", "科幻", "2022/5/21", "199779", R.raw.b3, 0});
            db.execSQL("insert into book values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{R.drawable.b4, "三体前传：球状闪电", "刘慈欣", "0", "0", "0", "科幻", "2022/5/21", "191385", R.raw.b4, 0});
        }
        bookCursor.close();

        db.execSQL(//创建保存评论的数据表
                "create table if not exists comment(" +
                        "id Integer primary key autoincrement," +
                        "bookname varchar(30) not null," +
                        "username varchar(30) not null," +
                        "comment varchar(30) not null)");
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        TextView textViewStepSecond = this.findViewById(R.id.tv_stepsecond);

        if (hasFocus) {//倒计时3s,跳转到LoginActivity
            int second = 4;
            CountDownTimer timer = new CountDownTimer(second * 1000,1000) {
                private int s = second;

                @Override
                public void onTick(long l) {
                    s--;
                    textViewStepSecond.setText(String.valueOf(s));
                }

                @Override
                public void onFinish() {
                    if (hasStep) {
                        hasStep = false;
                        return;
                    }
                    Intent intent = new Intent(StartActivity.this,LoginActivity.class);
                    startActivity(intent);
                }
            }.start();
        }
    }
}